<?php
/**
 * Copyright (c) 2018,2345
 * 摘    要：Account controller
 * 作    者：林幸亿
 * 修改日期：2018.05.10
 */
namespace App\Controllers;

use \Core\View;
use \App\Models\Account;
use \Library\Session;
use \Library\MysqlDb;
use \Library\UIHelper;

class AccountController extends \Core\Controller
{
    private $view;
    private $account;
    
    public function __construct()
    {
        /*
         *  session_status();
         *  _DISABLED = 0
         *  _NONE = 1
         *  _ACTIVE = 2
         */
        $this->account = new Account();
    }
    
    function __destruct()
    {
    }
    
     protected function before()
    {
        $this->view = View::renderTpl();
		$this->view->assign('menuLink', UIHelper::generateMenu(LINKS));
        $this->view->display('Layout/header.tpl.html');
    }
    
    protected function after()
    {
        $this->view->display('Layout/footer.tpl.html');
    }
    
    /**
     * 功    能：Show the index page
     * 
     * @return void
     * 修改日期：2018.05.10
     */
    public function indexAction()
    {
        $session = Session::getInstance();
        // Set to true if using https
        $session->start_session('_s', false);

        if($this->account->checkAuth() == false)
        {
            $loginMsg = $session->flash('loginMsg');
            $this->view->assign('message', $loginMsg);
            $this->view->assign('emailHighlight', $session->flash('emailHighlight'));
            $this->view->assign('emailError', $session->flash('emailError'));
            $this->view->assign('emailValue', $session->flash('emailValue'));
            $this->view->assign('passwordHighlight', $session->flash('passwordHighlight'));
            $this->view->assign('passwordError', $session->flash('passwordError'));
            $this->view->display('Common/login.tpl.html');
        }
        else
        {
            $message = '<p>当前登录为<strong>' . htmlentities($_SESSION['username']) . '</strong>.</p>'
                    . '<p>Currently logged in as <strong>' . htmlentities($_SESSION['username']) . '</strong>.</p>'
                    . '<p>您想更改用户吗？ <a href="'. BASE_URL . '/logout">退出</a>.</p>'
                    . '<p>Do you want to change user? <a href="'. BASE_URL . '/logout">Log out</a>.</p>';
            $this->view->assign('message', $message);
            $this->view->display('Common/logout.tpl.html');
        }
    }

     /*
      * 功    能：POST Login
      * @return void
      * 修改日期：2018.05.10
      */
    public function login()
    {
        if (! empty($_POST))
        {
            // keep track validation errors
            $emailError = null;
            $passwordError = null;

            // keep track post values
            $email = $_POST['email'];
            $password = $_POST['password'];

            // Validate input
            $valid = true;

            $session = Session::getInstance();
            // Set to true if using https
            $session->start_session('_s', false);
            $session->flash('emailValue', $email);
                            
            if (empty($email))
            {
                $emailError = '<p class="text-danger">请输入名称<br>Please enter Email Address</p>';
                $session->flash('emailHighlight', 'border border-danger');
                $session->flash('emailError', $emailError);
                $valid = false;
            }
            else if (! filter_var($email, FILTER_VALIDATE_EMAIL))
            {
                $emailError = '<p class="text-danger">请输入有效的电子邮件<br>Please enter a valid Email Address</p>';
                $session->flash('emailHighlight', 'border border-danger');
                $session->flash('emailError', $emailError);
                $valid = false;
            }

            if (empty($password))
            {
                $passwordError = '<p class="text-danger">请输入密码<br>Please enter Password</p>';
                $session->flash('passwordHighlight', 'border border-danger');
                $session->flash('passwordError', $passwordError);//die('test ' . $session->flash('passwordError'));
                $valid = false;
            }
            //$this->view->assign('output', var_dump($_POST) );
            //$this->view->display('Common/debug.tpl.html');

            // Update data if validation is successful and redirect to main page
            if ($valid)
            {
                $loginSuccess = $this->account->auth($email, $password);

                if($loginSuccess)
                {
                    $session->flash('listMsg', '<div class="alert alert-success alert-dismissible fade show" role="alert">Logged in successfully.
						<button type="button" class="close" data-dismiss="alert" aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
					</div>');
                    //Redirect back to main page
                    header("Location: " . BASE_URL . '/users');
                }
                else
                {
                    header("Location: " . BASE_URL . '/login');
                }

            }
            else
            {
                header("Location: " . BASE_URL . '/login');
            }

        }
        else
        {
            header("Location: " . BASE_URL . '/login');
        }
    }

    /*
     * 功    能：Logout
     * @return void
     * 修改日期：2018.05.10
     */
    public function logoutAction()
    {
        $session = Session::getInstance();
        // Set to true if using https
        $session->start_session('_s', false);
        
        // Unset all session values             
        $_SESSION = array();
        // get session parameters 
        //$params = session_get_cookie_params();
        // Delete the actual cookie. 
        /*
        setcookie(session_name(),
                '', time() - 42000, 
                $params["path"], 
                $params["domain"], 
                $params["secure"], 
                $params["httponly"]);
        */
        // Finally, destroy the session.
        session_destroy();
        //$session = Session::getInstance();
        // Set to true if using https
        //$session->start_session('_s', false);
        //$session->flash('loginMsg','<div class="alert alert-success" role="alert">Logout successful</div>');

        header("Location: " . BASE_URL . '/login');
    }
    
}
